<template>
  <div id="login">
    <el-row type="flex" justify="center">
      <el-col class="login-box">
        <h1 class="title">个人收支管理系统</h1>
        <el-form ref="loginForm" label-width="80px" :model="loginForm" :rules="loginRules">
          <el-form-item label="邮箱 :" prop="email">
            <el-input v-model="loginForm.email" clearable></el-input>
          </el-form-item>

          <el-form-item label="密码 :" prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              clearable
              @keyup.enter.native="loginSubmit('loginForm')"
            ></el-input>
          </el-form-item>
          <div class="btn-box">
            <el-button type="primary" @click="$router.push({name:'Register'})">去注册</el-button>
            <el-button type="success" @click="loginSubmit('loginForm')">登录</el-button>
          </div>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import jwtDecode from "jwt-decode";
export default {
  data() {
    return {
      loginForm: {
        email: "",
        password: "",
      },
      loginRules: {
        email: [
          { required: true, message: "请输入邮箱地址", trigger: "blur" },
          { type: "email", message: "请输入正确的邮箱地址", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "长度在 2 到 20 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    loginSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$http.post("/user/login", this.loginForm).then((res) => {
            if (res.data.success) {
              let { token } = res.data;
              // localStorage 添加 登录成功的 Token
              localStorage.setItem("moneyToken", token);
              // 解析 Token 得到对象
              let decoded = jwtDecode(token);
              // console.log(decoded); //{id: "5f2ec5ca5674e42dac850dc8", username: "12", email: "12@qq.com", iat: 1596904933, exp: 1596908533}

              this.$store.dispatch(
                "SetIsAuthenticated",
                !this.isEmpty(decoded)
              );

              this.$store.dispatch("SetUser", decoded);

              this.$message({
                type: "success",
                message: res.data.message,
              });
              this.$router.push({ name: "home" });
            } else {
              this.$message.error(res.data.message);
            }
          });
        } else {
          return false;
        }
      });
    },
    isEmpty(value) {
      return (
        value === undefined ||
        value === null ||
        (typeof value === "object" && Object.keys(value).length === 0) ||
        (typeof value === "string" && value.trim().length === 0)
      );
    },
  },
};
</script>

<style lang="less" scoped>
#login {
  .el-row {
    .login-box {
      margin-top: 50px;
      width: 300px;
      .title {
        text-align: center;
        font-weight: 300;
        margin-bottom: 15px;
      }
      .btn-box {
        text-align: center;
        .el-button {
          font-size: 15px;
        }
      }
    }
  }
}
</style>